package org.springblade.core.tool.utils;

import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PingKit {

    /**
     * 是否可以连接上
     *
     * @param ipList
     * @return
     */
    public static boolean isOk(String ipList) {
        try {
            if (ipList == null || ipList.trim().length() == 0) {
                return true;
            }
            String[] ips = ipList.split(",");
            for (String ip : ips) {
                if (ip == null || ip.trim().length() == 0) {
                    continue;
                }
                InetAddress address = InetAddress.getByName(ip.trim());
                if (!address.isReachable(4000)) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getIpFromUrl(String url){
        String regex = "\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);

        while (matcher.find()) {
           return  matcher.group();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(isOk("192.168.30.202"));
    }

}
